<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path;
%>
<script src="<%=basePath%>/Scripts/jquery.etree.js"></script>
<script src="<%=basePath%>/Scripts/jquery.etree.lang.js"></script>

<div style="height:100%; width:100%;">
   
    <ul id="departtree"></ul>
</div>
<div id="mm" class="easyui-menu" style="width: 120px;">
    <div onclick="javascript: $('#departtree').etree('create')" iconcls="icon-add">
        添加部门
    </div>
    <div onclick="javascript: $('#departtree').etree('edit')" iconcls="icon-edit">编辑部门</div>
    <div onclick="javascript: removedepart()" iconcls="icon-remove">
        取消部门
    </div>
    <div onclick="javascript: $('#dd').dialog();" iconcls="icon-remove">
        员工移动
    </div>
</div>
<div id="dd" class="easyui-dialog" title="该部门员工移动到—>" style="width: 30%">
    <input id="cc">
    <a href="javascript:saveuserdepart()" class="easyui-linkbutton" style=" width:100%;float: right" id="btnSave">确 认</a>
</div>
<script type="text/javascript">
    $(function () {
        //去掉多余的对话框
        if ($('.easyui-dialog').size() > 1) {
            $('.easyui-dialog').first().remove();
        }
        $.parser.onComplete = function () {
            $('.easyui-dialog').dialog('close');
        };
       
        $('#cc').combotree({
            url: '<%=basePath%>/system/department/getDepartmentTreeData',
            width:'100%',
            required: true,
            loadFilter: function (rows) {
                return convert(rows);
            }
        });
        $('#departtree').etree({
            url: '<%=basePath%>/system/department/getDepartmentTreeData',
            createUrl: '<%=basePath%>/system/department/addDepartment',
            updateUrl: '<%=basePath%>/system/department/updateDepartment',
            destroyUrl: '<%=basePath%>/system/department/deleteDepartment',
            //dndUrl: '/System/Department/MoveToOtherDepartment',
            checkbox: false,
            dnd: true,
            lines: true,
            onContextMenu: function (e, node) {
                e.preventDefault();
                $('#departtree').etree('select', node.target);
                $('#mm').menu('show', {
                    left: e.pageX,
                    top: e.pageY
                });
            },
            onDrop: function (target, source) {
                ERP_confirm('确定要调整部门结构吗？', function (r) {
                    debugger 
                    if (!r) {
                        return false;
                    } else {
                        var dd= $('#departtree').etree('getNode', target);
                        $.post('<%=basePath%>/system/department/moveToOtherDepartment', { id: source.id, dpid: $('#departtree').etree('getNode',target).id }, function (data) {
                            $('#departtree').etree('reload');
                        });
                    }
                })
            },
            loadFilter: function (rows) {
                return convert(rows);
            }
        });
    });
    function removedepart() {
        var node = $('#departtree').tree('getSelected');
        $.get('<%=basePath%>/system/department/departmentHasAnyUser?id=' + node.id, function (data) {
            if (data == "1") {
                ERP_alertInfo('请先将部门下面的员工移动到其他部门处!');
                return;
            } else {
                ERP_confirm('确定要取消该部门吗？', function (r) {
                    if (r)
                        $('#departtree').etree('destroy');
                });

            }
        });
    }

    function saveuserdepart() {
        var spid = $('#departtree').tree('getSelected').id;
        var dpid = $('#cc').combotree('getValue');
        if (spid == dpid) {
            ERP_alertError('不能移动到相同的部门！');
            return;
        }
        $.post('<%=basePath%>/system/department/moveUserToDepartment?id=' + spid, { dpid: dpid }, function (data) {
            if (data == "") {
                ERP_alertInfo('移动成功!');
            }
        });
    }
</script>



